import 'dart:collection';
import 'dart:convert';

import 'package:webfeed/domain/rss_content.dart';

import 'Api.dart';
import 'Network.dart';

class NetworkInterface {
  /// 根据关键字搜索文章
  static Future<List<dynamic>> search(String value) async {
    String str = await Network.get(Api.search, keyParams: {"value": value});
    var data = json.decode(str);
    return data['data'];
  }

  /// 根据标签名查找标签包含的文章
  static Future<List<dynamic>> getTagArticle(String value) async {
    String str = await Network.get(Api.tag_name, listParams: [value]);
    return json.decode(str);
  }

  /// 获得文章标签
  static Future<List<String>> getTags() async {
    String str = await Network.get(Api.meta);
    var tags = json.decode(str);
    List<String> obj = tags['tags'].cast<String>();
    return obj;
  }

  /// 获得文章分类
  static Future<List<String>> getCategories() async {
    String str = await Network.get(Api.meta);
    var tags = json.decode(str);
    List<String> obj = tags['meta']['categories'].cast<String>();
    return obj;
  }

  /// 获得文章分类、文章分类里面的文章
  static Future<dynamic> getCategory() async {
    String str = await Network.get(Api.category);
    var category = json.decode(str);
    return category;
  }

  /// 获得文章时间线
  static Future<Map<String, dynamic>> getTimeline() async {
    String str = await Network.get(Api.timeline);
    var category = json.decode(str);
    return category;
  }

  /// 获得评论
  static Future<List<dynamic>> getComment(String articleId) async {
    Map<String,String> key = HashMap();
    key["linkPath"] = articleId;
    var net = await Network.getLocalInterface(Api.queryComment,keyParams: key);
    List<dynamic> list = json.decode(net);
    return list;
  }

  /// 添加评论
  static Future<String> addComment(String name,String content,String articlePath,String type,String replyId) async {
    Map<String,String> key = HashMap();
    key["name"] = name;
    key["content"] = content;
    key["article_path"] = articlePath;
    key["type"] = type;
    key["reply_id"] = replyId;
    String result = await Network.getLocalInterface(Api.addComment,keyParams: key);
    return result;
  }


  /// 添加博客
  static addBlog(String title, String author, String link, String categories, String pubDate, String description, String content) async {
    Map<String,String> key = HashMap();
    key["title"] = title;
    key["author"] = author;
    key["link"] = link;
    key["categories"] = categories;
    key["pubDate"] = pubDate;
    key["description"] = description;
    // key["content"] = content;
    String result = await Network.getLocalInterface(Api.addBlog,keyParams: key);
    return result;
  }


}
